import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/cabinet/pages/cabinet_list_info_edit/view.dart';
import 'package:honeypotflutter/modules/cabinet/views/cabinet_list_header.dart';
import 'package:honeypotflutter/public/views/public_appBar.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_card.dart';
import '../../model/cabinet_info_status_model.dart';
import '../../views/cabinet_list_info_content.dart';
import '../../views/cabinet_list_info_title.dart';
import 'logic.dart';

class CabinetListInfoPage extends StatefulWidget {
  const CabinetListInfoPage({Key? key}) : super(key: key);

  @override
  State<CabinetListInfoPage> createState() => _CabinetListInfoPageState();
}

class _CabinetListInfoPageState extends State<CabinetListInfoPage> {
  final CabinetListInfoLogic logic = Get.put(CabinetListInfoLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HLAppBar(navigationTitle: "柜机管理"),
      body: MGCard(
        margin: EdgeInsets.only(left: 16, right: 16, top: 12),
        children: [
          CabinetListHeader(
            deviceID: logic.itemModel.disPlayNo ?? "",
            deviceName: logic.itemModel.deviceAddress ?? "",
          ),
          Container(
            margin: EdgeInsets.only(top: 20, bottom: 20),
            width: double.infinity,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(4),
              border: Border.all(color: MGColors.splitColor, width: 0.5),
            ),
            child: GetBuilder<CabinetListInfoLogic>(builder: (controller) {
              return _fromWidget();
            }),
          )
        ],
      ),
    );
  }

  Column _fromWidget() {
    List<Widget> widgetList = [CabinetListInfoTitle()];
    List<Widget> list = logic.infoModelList.map((e) {
      CabinetInfoStatusModel? model = logic.findStatus(e.boardNo);
      if (e.boardNo == 0) {
        return CabinetListInfoContent(isEdit: false, number: "A", appoint: "禁止指定A柜", status: "禁用");
      }
      return CabinetListInfoContent(
        number: String.fromCharCode(65 + (e.boardNo ?? 0)),
        appoint: (model != null && model.courierPhone != null) ? model.courierPhone : "",
        status: (model != null && model.isActive == true) ? "启用" : "禁用",
        editAction: () async {
          bool result = await Get.to(CabinetListInfoEditPage(), arguments: {"info": e, "status": model});
          if (result == true) {
            logic.request();
          }
        },
      );
    }).toList();
    widgetList.addAll(list);

    return Column(
      children: widgetList,
    );
  }

  @override
  void dispose() {
    Get.delete<CabinetListInfoLogic>();
    super.dispose();
  }
}
